// pages/shop_flow/flow.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',
    list: '',
    dataList: [],
  },
  init: function() {
    let self = this;
    this.viewExpress(function(res) {
      if (res.data.content.data) {
        for (let i = 0; i < res.data.content.data.length; i++) {
          let phone_str = res.data.content.data[i].context;
          //--手机号码
          let reg_phone = /\b(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}/g;
          let phone_num = phone_str.match(reg_phone);
          let phone_html = " [" + (Array.isArray(phone_num) ? phone_num[0] : phone_num) + "] ";
          phone_str = phone_str.replace(reg_phone, phone_html);

          //--电话号码
          let reg_tel = /(\d{3,4}-)\d{7,8}/g;
          let tel_num = phone_str.match(reg_tel);
          let tel_html = " [" + (Array.isArray(tel_num) ? tel_num[0] : tel_num) + "] ";
          phone_str = phone_str.replace(reg_tel, tel_html);

          res.data.content.data[i].context = phone_str;
          res.data.content.data[i].phone = Array.isArray(phone_num) ? phone_num[0] : phone_num;
          res.data.content.data[i].tel = Array.isArray(tel_num) ? tel_num[0] : tel_num;
        }
      } else {
        api.toast('暂无物流信息');
      }
      self.setData({
        list: res.data,
        dataList: res.data.content.data
      });
      console.log(res);
    });
  },
  viewExpress: function(callback) {
    let self = this;
    api.ajax(service.url('buyViewExpress'), {
      order_id: this.data.order_id
    }, function(res) {
      console.log("flow：" + JSON.stringify(res));
      if (res.code == 0) {
        callback(res);
      } else {
        api.toast(res.message || '数据出现错误！');
      }
    });
  },
  //--打开修改物流信息页面
  openFlowEdit: function(et) {
    let self = this;
    wx.navigateTo({
      url: '../shop_send/send?flowEdit=true&order_id=' + this.data.order_id,
    })
  },
  //--复制
  shopFlowCopy: function(et) {
    let self = this;
    let _expresssn = et.currentTarget.dataset.expresssn;
    wx.setClipboardData({
      data: _expresssn
    });
  },
  dialphone: function(et) {
    let index = +et.currentTarget.dataset.index,
      _phone = '';
    if (dataList[index].phone == null && dataList[index].tel == null) {
      return;
    }
    if (dataList[index].phone != null) {
      _phone = dataList[index].phone;
    }
    if (dataList[index].phone == null && dataList[index].tel != null) {
      _phone = dataList[index].tel;
    }
    wx.makePhoneCall({
      phoneNumber: _phone
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      order_id: options.order_id
    });
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
